package cn.wen.eeayjoke.utils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.disposables.DisposableHelper;

/**
 * 倒计时的帮助类
 */

public class CountDownHelper {
    private static final String TAG = "CountDownHelper";
    /**
     * 倒计时间
     */
    private long time;
    private Observable<Long> mObservable;
    // 订阅
    private CancelObserver<Long> defaultObserver;
    private OnCountDownListener mDownListener;

    public CountDownHelper(long time) {
        this.time = time;
        createCountdown();
    }

    private void createCountdown() {
        // 参数说明：
        // 参数1 = 事件序列起始点；
        // 参数2 = 事件数量；
        // 参数3 = 第1次事件延迟发送时间；
        // 参数4 = 间隔时间数字；
        // 参数5 = 时间单位
        mObservable = Observable.intervalRange(1, time, 0, 1, TimeUnit.SECONDS);
        // 1 2 3 4 这个样子
    }

    /**
     * 开始倒计时
     */
    public void start() {
        defaultObserver = new CancelObserver<Long>() {
            @Override
            public void onStart() {
                if (mDownListener != null) {
                    mDownListener.onStart();
                }
            }

            @Override
            public void onNext(Long value) {
                long ctime = time - value;
                if (mDownListener != null) {
                    mDownListener.onCountdown(ctime);
                }
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                if (mDownListener != null) {
                    mDownListener.onComplete();
                }
            }

            @Override
            public void onComplete() {
                if (mDownListener != null) {
                    mDownListener.onComplete();
                }
            }
        };
        mObservable.observeOn(AndroidSchedulers.mainThread()).subscribe(defaultObserver);
    }

    /**
     * 取消
     */
    public void cancel() {
        if (defaultObserver != null) {
            defaultObserver.cancel();
        }
    }

    private static abstract class CancelObserver<T> implements Observer<T> {

        private Disposable s;

        @Override
        public final void onSubscribe(Disposable s) {
            if (DisposableHelper.validate(this.s, s)) {
                this.s = s;
                onStart();
            }
        }

        /**
         * 取消
         */
        public void cancel() {
            Disposable s = this.s;
            this.s = DisposableHelper.DISPOSED;
            if (!s.isDisposed()) {
                s.dispose();
            }

        }

        /**
         * 开始
         */
        protected void onStart() {
        }

    }

    public interface OnCountDownListener {
        /**
         * 倒计
         */
        void onCountdown(long countdownTime);

        /**
         * 完成
         */
        void onComplete();

        void onStart();
    }

    /**
     * 设置倒计时回调监听
     */
    public void setDownListener(OnCountDownListener listener) {
        mDownListener = listener;
    }
}
